<template>
  <view class="pre-checkin-list">
    <scroll-view
      class="scroll-view"
      scroll-y
      refresher-enabled
      :refresher-triggered="isRefreshing"
      @refresherrefresh="onRefresh"
      @scrolltolower="onLoadMore"
    >
      <view class="list">
        <view v-for="(item, index) in list" :key="index" class="list-item" @click="handleDetail(item.registerId)">
          <view class="card-header">
            <text class="room-name">{{ getApartmentName(item.room) }}</text>
            <text :class="['status-tag', getStatusClass(item.status)]">{{ getStatusText(item.status) }}</text>
          </view>
          <view class="card-content">
            <view class="info-row">
              <text class="label">登记时间：</text>
              <text class="value">{{ formatDate(item.createTime) }}</text>
            </view>
            <!-- 可以根据需要添加更多信息行 -->
          </view>
          <view class="card-footer">
            <u-icon name="arrow-right" color="#CCCCCC" size="24"></u-icon>
          </view>
        </view>
        <view v-if="loading" class="loading">加载中...</view>
        <view v-if="!hasMore && list.length > 0" class="no-more">没有更多数据</view>
        <view v-if="!loading && list.length === 0" class="empty-list">
          <u-empty mode="list" text="暂无预约记录"></u-empty>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import dayjs from 'dayjs'
import { myPreCheckInList } from '@/api/preCheckIn'
import { isNotEmpty } from '@/utils'

export default {
  data() {
    return {
      list: [],
      page: 1,
      pageSize: 10,
      loading: false,
      hasMore: true,
      isRefreshing: false
    }
  },
  
  onLoad() {
    this.loadData()
  },

  methods: {
    async loadData(isRefresh = false) {
      if (isRefresh) {
        this.page = 1
        this.hasMore = true
      }

      if (!this.hasMore || this.loading) return

      this.loading = true
      try {
        let { data } = await myPreCheckInList({
          page: this.page,
          pageSize: this.pageSize
        })
        if (isRefresh) {
          this.list = data
        } else {
          this.list = [...this.list, ...data]
        }

        this.hasMore = data.length === this.pageSize
        this.page++
      } catch (error) {
        console.log(error);
        
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
        if (isRefresh) {
          this.isRefreshing = false
        }
      }
    },
    getApartmentName(room) {
      try {
        if (isNotEmpty(room)) {
          const buildingName = isNotEmpty(room.buildingName) ? room.buildingName : ''
          const name = isNotEmpty(room.name) ? room.name : ''
          const fullName = buildingName + name
          return isNotEmpty(fullName) ? fullName : '暂未登记房间'
        }
        return '暂未登记房间'
      } catch (error) {
        console.log(error);
        return '--'
      }
    },

    handleDetail(registerId) {
      this.$tab.navigateTo(`/pages/mine/pre-checkin-list/detail?registerId=${registerId}`)
    },

    onRefresh() {
      this.isRefreshing = true
      this.loadData(true)
    },

    onLoadMore() {
      this.loadData()
    },

    formatDate(date) {
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    },
    
    getStatusText(status) {
      const statusMap = {
        '0': '待审核',
        '1': '审核通过',
        '2': '审核驳回',
        '4': '办理入住成功'
      }
      return statusMap[status] || '未知状态'
    },
    
    getStatusClass(status) {
      const classMap = {
        '0': 'status-pending',
        '1': 'status-approved',
        '2': 'status-rejected',
        '4': 'status-success'
      }
      return classMap[status] || ''
    }
  }
}
</script>

<style lang="scss" scoped>
.pre-checkin-list {
  height: 100vh;
  background: #f5f5f5;
  
  .scroll-view {
    height: 100%;
    padding: 10px;
  }

  .list-item {
    margin-bottom: 15px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 15px;
      border-bottom: 1px solid #f0f0f0;
      
      .room-name {
        font-size: 16px;
        font-weight: 600;
        color: #333;
      }
      
      .status-tag {
        padding: 4px 8px;
        border-radius: 4px;
        font-size: 12px;
      }
      
      .status-pending {
        background-color: #e6f7ff;
        color: #1890ff;
      }
      
      .status-approved {
        background-color: #f6ffed;
        color: #52c41a;
      }
      
      .status-rejected {
        background-color: #fff2f0;
        color: #ff4d4f;
      }
      
      .status-success {
        background-color: #f0f5ff;
        color: #2f54eb;
      }
    }
    
    .card-content {
      padding: 15px;
      
      .info-row {
        display: flex;
        margin-bottom: 8px;
        align-items: center;
        
        .label {
          color: #666;
          font-size: 14px;
          width: 80px;
        }
        
        .value {
          color: #333;
          font-size: 14px;
          flex: 1;
        }
      }
    }
    
    .card-footer {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      padding: 10px 15px;
      background-color: #fafafa;
    }
  }

  .loading,
  .no-more {
    text-align: center;
    color: #999;
    padding: 10px 0;
    font-size: 14px;
  }
  
  .empty-list {
    padding: 40px 0;
  }
}
</style>
